package com.supermap.util.network;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import rx.Subscriber;

/**
 * 自定义订阅
 * Created by hjz on 2017/8/28.
 */

public abstract class BaseSubscriber<T> extends Subscriber<T> {
    private HttpOnNextListener httpOnNextListener;
    private Context mContext;

    public BaseSubscriber(Context context) {
        this.mContext = context;
    }


    @Override
    public void onCompleted() {
        Log.d("测试数据", "onCompleted");
    }

    @Override
    public void onError(Throwable e) {
        if (e instanceof SocketTimeoutException) {
            Toast.makeText(mContext, "网络中断，请检查您的网络状态", Toast.LENGTH_SHORT).show();
        } else if (e instanceof ConnectException) {
            Toast.makeText(mContext, "网络中断，请检查您的网络状态", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(mContext, "错误" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
        Log.d("测试数据", "onError");
        onFailed();
    }

    @Override
    public void onNext(T t) {
        Log.d("测试数据", "onNext");
        if (t != null) {
            onResult(t);
        }
    }

    protected abstract void onResult(T t);

    public void onFailed() {

    }

    /**
     * 成功回调接口
     */
    public interface HttpOnNextListener<T> {
        void onNext(T t);
    }
}
